
import { ResolvedFn, RejectedFn} from "../types/index";

export default class InterceptorManager<T>{

    private inerceptors: Array<Interceptor<T> | null>

    constructor() {
        this.inerceptors = []
    }

    use(resolved: ResolvedFn<T>, rejected: RejectedFn): number{
        this.inerceptors.push({
            resolved,
            rejected
        })
        return this.inerceptors.length - 1
    }

    forEach(fn: (interceptor: Interceptor<T>)=>void): void{
        this.inerceptors.forEach(interceptor=>{
            if(interceptor !== null){
                fn(interceptor)
            }
        })
    }

    ejected(id: number): void{
        if(this.inerceptors[id]){
            this.inerceptors[id] = null
        }
    }

}

interface Interceptor<T>{
    resolved: ResolvedFn<T>
    rejected: RejectedFn
}